<?php
namespace Modules\Home\Services;

use Common\Models\UserAchievementLogModel;

class UserAchievementLogService {
  /*
   * 计算左右区总业绩
   */
  /*
  public function sumUserAchievement($user_id) {
    $data = [];

    $data['amount_left'] = UserAchievementLogModel::sum([
      'type = 1 AND user_id = ' . $user_id,
      'column' => 'amount_left'
    ]);

    $data['amount_right'] = UserAchievementLogModel::sum([
      'type = 1 AND user_id = ' . $user_id,
      'column' => 'amount_right'
    ]);

    return ['code' => 0, 'sum_achievement' => $data];
  }
  */

  public function create($data) {
    $userAchievementLogModel = new UserAchievementLogModel($data);
    try {
      if(!$userAchievementLogModel -> create()) {
        $messages = $userAchievementLogModel -> getMessages();
        throw new \Exception($messages[0] -> getMessage());
      }
    } catch(\Exception $e) {
      return ['code' => -1, 'message' => $e -> getMessage()];
    }

    return ['code' => 0, 'user_achievement_log' => $userAchievementLogModel];
  }
}
?>